<?php defined('SYSPATH') or die('No direct script access.');
 
class Model_Categories extends ORM {
	protected $_table_name = 'categories';

	public function get_main($type) {
		return DB::select()
			->from('categories')
			->where('parent_id', '=', '0')
			->and_where('type', '=', $type)
			->order_by('sort', 'DESC')
			->as_object()
			->execute();
	}

	public function get_childrens($parent_id) {
		return DB::select()
			->from('categories')
			->where('parent_id', '=', $parent_id)
			->order_by('sort', 'DESC')
			->as_object()
			->execute();
	}

	public function get_one($id) {
		return DB::select()
			->from('categories')
			->where('id', '=', $id)
			->as_object()
			->execute()
			->current();
	}

	public function get_last_one($type) {
		return DB::select()
			->from('categories')
			->where('type', '=', $type)
			->and_where('parent_id', '!=', '0')
			->order_by('sort', 'DESC')
			->as_object()
			->execute()
			->current();
	}
}